package com.snails.app.glide;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import androidx.annotation.NonNull;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.gif.GifOptions;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

/**
 * Glide设置：
 *  注意：
 *      1、此文件，在包名下，即一级目录
 *      2、app模块的build.gradle中添加下面依赖：
 *         annotationProcessor "com.github.bumptech.glide:compiler:4.9.0"
 *      3、使用GlideApp.with(fragment).load(myUrl).into(imageView);
 *         代替Glide.with(fragment).load(myUrl).into(imageView);
 * @author lawrence
 * @date 2019-04-25 17:35
 */

public class SnailsGlideModule extends AppGlideModule {
    private static RequestOptions options = null;

    public static RequestOptions cloneDefaultOptions() {
        return options != null ? options.clone() : new RequestOptions();
    }

    // -----------------------------------------------------------------------------
    protected RequestOptions options() {
        return null;
    }
    protected void snailsApplyOptions(@NonNull Context context, @NonNull GlideBuilder builder) { }


    // -----------------------------------------------------------------------------
    private final int diskCacheSizeBytes = 1024 * 1024 * 150;       // 磁盘缓存大小：150 MB
    private static String sGlideCache = null;                       // Glide缓存文件地址

    private RequestOptions SnailsOptions() {
        final RequestOptions requestOptions = options();
        final RequestOptions resultOptions = requestOptions != null ? requestOptions : new RequestOptions()
                .placeholder(new ColorDrawable(Color.GRAY))
                .diskCacheStrategy(DiskCacheStrategy.ALL)       // 图片磁盘缓存策略
                .encodeQuality(90)                              // 质量压缩
                .format(DecodeFormat.PREFER_RGB_565)            // 解码格式，PREFER_ARGB_8888最高
                // gif图，去掉默认占位图片，否则会露背景
                .set(GifOptions.DECODE_FORMAT, DecodeFormat.PREFER_RGB_565);
        return SnailsGlideModule.options = resultOptions;
    }

    @Override   // 不检测manifest中的Glide设置
    public boolean isManifestParsingEnabled() {
        return false;
    }

    @Override
    public void applyOptions(@NonNull Context ctx, @NonNull GlideBuilder builder) {
        super.applyOptions(ctx, builder);
        final SnailsGlideCacheFactory factory =
                new SnailsGlideCacheFactory(ctx, "imageCache", diskCacheSizeBytes);
        sGlideCache = factory.getFolder();  // Glide缓存文件地址
        builder.setDiskCache(factory);
        final RequestOptions requestOptions = SnailsOptions();
        if (requestOptions != null) builder.setDefaultRequestOptions(requestOptions);
        snailsApplyOptions(ctx, builder);
    }

    /**
     * 得到Glide的缓存文件夹
     */
    public static String getFolder() {
        return sGlideCache;
    }

}
